﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Castle.ActiveRecord;
using NHibernate.Criterion;
using LIB;

public partial class admin_tin_tuc_Editor : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            using (new SessionScope())
            {
                if (!IsPostBack)
                {
                    int catID = ConvertUtility.ToInt32(Request.QueryString["catID"]);
                    hidCatID.Value = catID.ToString();
                    Category_Info catInfo = Category_Info.Find(catID);
                    if (catInfo != null)
                    {
                        litPos.Text = "<a href='/admin/tin-tuc/default.aspx'>Module Quản trị tin tức</a> >>";
                        catDeQuyNguoc[] lstCat = catDeQuyNguoc.getLinkCat(catID);
                        for (int i = lstCat.Length - 1; i > -1; i--)
                        {
                            litPos.Text += "<a href='/admin/tin-tuc/default.aspx?id=" + lstCat[i].id + "'> " + lstCat[i].catName + " </a> >>";
                        }
                    }
                    int id = ConvertUtility.ToInt32(Request.QueryString["id"]);

                    if (id > 0)
                    {
                        hidID.Value = id.ToString();
                        initForm(true);
                    }
                    else
                    {
                        initForm(false);
                    }
                }
            }
          
            fail.Visible = false;
        }
        catch (Exception ex) { }
    }
    protected void initForm(bool isEdit)
    {
        if (isEdit)
        {
            btnAdd.Visible = false;
            btnEdit.Visible = true;
            litPos.Text += "Sửa ";
            fillData();
        }
        else
        {
            btnAdd.Visible = true;
            btnEdit.Visible = false;
            litPos.Text += "Thêm mới  ";
        }
    }


    protected void fillData()
    {
        try
        {
            int catID = ConvertUtility.ToInt32(hidCatID.Value);
            Content_Info contentInfo = Content_Info.Find(ConvertUtility.ToInt32(hidID.Value));
            imgAvatar.ImageUrl = App_Constant.PATH_UP_CONTENT + "/" + contentInfo.contentAvatar;
            hidAvatar.Value = contentInfo.contentAvatar;
            txtTitle.Text = contentInfo.contentTitle;
            txtDes.Text = contentInfo.contentSummary;
            radNewsContent.Value = contentInfo.contentDes;
            contentInfo.contentCatID = Category_Info.Find(catID);
        }
        catch (Exception ex) { }
    }
    protected Content_Info getContentInfo(Content_Info contentInfo)
    {
        int catID = ConvertUtility.ToInt32(hidCatID.Value);
        contentInfo.contentCatID = Category_Info.Find(catID);
        contentInfo.contentAvatar = fileUpAvatar.HasFile ? App_File.tienToFile() + "." + fileUpAvatar.FileName : hidAvatar.Value;
        contentInfo.contentTitle = txtTitle.Text;
        contentInfo.contentSummary = StringUtility.RemoveInvalidHTMLTags(txtDes.Text, null);
        contentInfo.contentDes = radNewsContent.Value;
        contentInfo.contentDate = DateTime.Now;
        contentInfo.contentIsVisible = true;
        contentInfo.contentOrder = 1;
        return contentInfo;
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        try
        {
            Content_Info contentInfo = getContentInfo(new Content_Info());
            if (fileUpAvatar.HasFile)
                App_File.uploadFileOnSerVer(fileUpAvatar, App_Constant.PATH_UP_CONTENT + "/", 150, 100);
            using (new SessionScope())
                contentInfo.Create();
            Response.Redirect("/admin/tin-tuc/default.aspx?id="+hidCatID.Value);
        }
        catch (Exception ex)
        {
            fail.Visible = true;
        }
    }
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        try
        {
            Content_Info contentInfo = getContentInfo(Content_Info.Find(ConvertUtility.ToInt32(hidID.Value)));
            if (fileUpAvatar.HasFile)
                App_File.uploadFileOnSerVer(fileUpAvatar, App_Constant.PATH_UP_IMAGE + "/", 150, 100);
            using (new SessionScope())
                contentInfo.SaveAndFlush();
            Response.Redirect("/admin/tin-tuc/default.aspx?id=" + hidCatID.Value);
        }
        catch (Exception ex)
        {
            fail.Visible = true;
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        if (hidID.Value != "")
            fillData();
        else
            ControlUtility.EmptyTextBoxes(this);

    }
}